package cn.qiyu5522.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.qiyu5522.domain.DTO.UserInfoDTO;
import cn.qiyu5522.domain.VO.UserInfoVO;
import cn.qiyu5522.domain.entity.ResponseResult;
import cn.qiyu5522.domain.entity.SystemException;
import cn.qiyu5522.domain.entity.User;
import cn.qiyu5522.enums.AppHttpCodeEnum;
import cn.qiyu5522.mapper.UserMapper;
import cn.qiyu5522.service.UserService;
import cn.qiyu5522.utils.BeanCopyUtils;
import cn.qiyu5522.utils.RedisCache;
import cn.qiyu5522.utils.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Tonkey
 * @since 2024-04-17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {



    @Resource
    private RedisCache redisCache;

    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public ResponseResult userInfo() {
        Long loginUserId = SecurityUtils.getLoginUserId();
        User user = getById(loginUserId);
        UserInfoVO userInfoVO = BeanCopyUtils.copyBean(user, UserInfoVO.class);
        return ResponseResult.okResult(userInfoVO);
    }

    @Override
    public ResponseResult updateUserInfo(UserInfoDTO userInfoVO) {
        // DTO转实体类
        User user = BeanCopyUtils.copyBean(userInfoVO, User.class);
        updateById(user);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult register(UserInfoDTO userInfoDTO) {
        //DTO 转实体类
        User user = BeanCopyUtils.copyBean(userInfoDTO, User.class);
        // 用户名、昵称、邮箱 不能重复
        if(isExistUserName(user.getUserName())){
            throw new SystemException(AppHttpCodeEnum.USERNAME_EXIST);
        }
        if(isExistNickName(user.getNickName())){
            throw new SystemException(AppHttpCodeEnum.NICKNAME_EXIST);
        }
        if(isExistEmail(user.getEmail())){
            throw new SystemException(AppHttpCodeEnum.EMAIL_EXIST);
        }
        String encode = passwordEncoder.encode(user.getPassword());
        user.setPassword(encode);
        save(user);
        return ResponseResult.okResult();
    }

    private boolean isExistEmail(String email) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName,email);
        User one = getOne(queryWrapper);
        return ObjectUtil.isNotEmpty(one);
    }

    private Boolean isExistUserName(String username){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName,username);
        User one = getOne(queryWrapper);
        return ObjectUtil.isNotEmpty(one);
    }

    private Boolean isExistNickName(String nickname){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName,nickname);
        User one = getOne(queryWrapper);
        return ObjectUtil.isNotEmpty(one);
    }


}
